import json
import time
import os
import csv

from utils.file_tool import FileTool
from config import HEADERS, BASE_URL
from api.common_api import APIClient

BASE_PATH = os.path.dirname(os.path.dirname(__file__))


def get_fail_url(filename):
    result = []
    seen_pages = set()

    with open(BASE_PATH + '/file/' + filename, newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            # (2) 如果source_page为空，跳过当前行
            source_page = row['source_page'].strip()
            if not source_page:
                continue

            # (去重) 如果source_page已经处理过，跳过
            if source_page in seen_pages:
                continue
            seen_pages.add(source_page)

            # (3) 如果source_origin_link 为空或者 NULL，值等同于source_link
            origin_link = row.get('source_origin_link', '').strip()
            if not origin_link or origin_link.upper() == 'NULL':
                origin_link = row['source_link']

            # (1) 封装成字典并添加到结果列表
            item = {
                'source_link': row['source_link'],
                'source_page': source_page,
                'source_origin_link': origin_link,
                'resource_name': row['download_name']
            }
            result.append(item)

    return result


def write_data_json(json_data):
    # 写入json
    with open(BASE_PATH + '/data/urls.json', 'w', encoding='utf-8') as f:
        json.dump(json_data, f, ensure_ascii=False, indent=2)


def sends_requests(api_client, file_name):
    """
    批量发起云收藏
    :param api_client: api对象
    :param file_name: 资源链接文件
    :return:
    """
    print(f"************************批量云收藏执行开始*************************")
    # 1. 获取数据
    data = FileTool.read_file2(file_name)
    for item in data:
        # 避免接口请求太高频繁，暂时延迟1s
        time.sleep(5)
        # 2. 批量提交离线下载（云收藏）资源
        json_data = {
            "resource_list": [
                {
                    "resource_url": item.get('source_link'),
                    "resource_origin_url": item.get('source_origin_link'),
                    "resource_name": item.get('resource_name')
                }
            ],
            "resource_page": item.get('source_page'),
            "resource_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
        }
        response = api_client.post("/offline/batch_download", headers=HEADERS, json=json_data)
        print('批量提交离线下载接口请求地址:{}，解析资源:{},响应:{}'.format(BASE_URL + '/offline/batch_download', item, response))

    print(f"*************批量云收藏执行完毕，此次执行数量{len(data)}*************")


def group_data(lst):
    """
    数据分组
    :param lst:
    :return:
    """
    chunk_size = 100

    chunks = [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]
    return chunks
    # 打印分组结果
    # for idx, chunk in enumerate(chunks):
    #     print(f"第{idx + 1}组，共{len(chunk)}条: {chunk}")


if __name__ == '__main__':
    # 第一步获取云收藏失败数据
    # result = get_fail_url('storage_download_online_err.csv')

    # # 生成json
    # write_data_json(result)

    # 第二步，批量执行云收藏
    api_client = APIClient(BASE_URL)
    sends_requests(api_client, 'urls.json')
